package com.zhentao.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.model.entity.system.SysUser;
import com.zhentao.model.entity.user.UserInfo;
import com.zhentao.model.vo.common.Result;
import com.zhentao.mapper.OrderInfoMapper;
import com.zhentao.mapper.OrderStatisticsMapper;
import com.zhentao.model.dto.order.OrderStatisticsDto;
import com.zhentao.model.entity.order.OrderInfo;
import com.zhentao.model.entity.order.OrderStatistics;
import com.zhentao.model.vo.order.OrderInfoVo;
import com.zhentao.model.vo.order.OrderStatisticsVo;
import com.zhentao.service.OrderInfoService;
import com.zhentao.service.SysUserInfoService;
import com.zhentao.service.SysUserService;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 20183
* @description 针对表【order_info(订单)】的数据库操作Service实现
* @createDate 2024-08-20 09:39:17
*/
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
    implements OrderInfoService{
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Autowired
    private SysUserInfoService sysUserInfoService;
    /**
     * 获取订单统计数据
     * @param orderStatisticsDto
     * @return
     */
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //查询统计到的结果数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectListOs(orderStatisticsDto);

        //统计日期列表
        List<String> dateList = orderStatisticsList.stream().map(orderStatistics ->
                DateUtil.format(orderStatistics.getOrderDate(),"yyyy-MM-dd")).collect(Collectors.toList());

        //统计金额列表
        List<BigDecimal> amountList = orderStatisticsList.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());

        //OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        return orderStatisticsVo;
    }

    /**
     * 查询所有订单---------订单列表
     *
     * @param orderInfoVo
     * @return
     */
    @Override
    public Result selectAllOrder(OrderInfoVo orderInfoVo) {
        Page<OrderInfo> page = this.page(new Page<>(orderInfoVo.getPageNum(), orderInfoVo.getPageSize()), Wrappers.<OrderInfo>lambdaQuery()
                .like(StringUtils.checkValNotNull(orderInfoVo.getNickName()),OrderInfo::getNickName,orderInfoVo.getNickName())
                .like(StringUtils.checkValNotNull(orderInfoVo.getReceiverName()),OrderInfo::getReceiverName,orderInfoVo.getReceiverName())
        );
        page.getRecords().forEach(Item->{
            // 转换
            if (Item.getUserId()!=null){
                Item.setUsername(sysUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getId,Item.getUserId())).getUsername());
            }
        });
        return Result.build(page, 200, "操作成功");
    }

    /**
     * 修改订单状态
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(Integer id) {
        return orderStatisticsMapper.updateStatus(id)?Result.build(null, 200, "发货成功"):Result.build(null, 500, "发货失败");
    }

    /**
     * 查询订单
     * @param orderInfoVo
     * @return
     */
    @Override
    public Result selectOrder(OrderInfoVo orderInfoVo) {
        PageHelper.startPage(orderInfoVo.getPageNum(),orderInfoVo.getPageSize());
        List<OrderInfo> orderInfos = orderInfoMapper.selectOrder(orderInfoVo);
        PageInfo<OrderInfo> orderInfoPageInfo = new PageInfo<>(orderInfos);
        return Result.build(orderInfoPageInfo,200,"操作成功");
    }
}




